package com.hmkj.web.controller.pay;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.utils.OrderNoUtils;
import com.hmkj.core.api.ninepai.RpmUtils;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.po.amount.Recharge;
import com.hmkj.core.service.amount.RechargeService;
import com.hmkj.core.service.user.UserBankService;
import com.hmkj.core.service.user.UserService;
import com.hmkj.web.controller.GenericController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 九派支付
 *
 * @author Vector
 * @create 2018-01-12 10:18
 */

@Api(description = "九派支付")
@Slf4j
@RestController
@RequestMapping(GenericController.MEMBER_URL + "/pay/ninepai")
public class NinePaiPayController extends GenericController<Integer, Recharge> {

    @Resource
    private UserService userService;

    @Resource
    private UserBankService userBankService;

    @Resource
    private RechargeService rechargeService;

    /**
     * 快捷绑卡
     *
     * @param phone      预留手机
     * @param cardNo     银行卡号
     * @param cardType   银行卡类别 0 储蓄卡  1 信用卡
     * @param expireDate 贷记卡有效期
     * @param cvn2       贷记卡cnv2
     */
    @ApiOperation(value = "快捷绑卡", notes = "快捷绑卡")
    @ResponseBody
    @RequestMapping(value = "/bindCard", method = RequestMethod.POST)
    public ResultEntity bindCard(@ApiParam("预留手机") @RequestParam String phone,
                                 @ApiParam("银行卡号") @RequestParam String cardNo,
                                 @ApiParam("银行卡类别 0 储蓄卡  1 信用卡") @RequestParam String cardType,
                                 @ApiParam("贷记卡有效期") @RequestParam(required = false) String expireDate,
                                 @ApiParam("贷记卡cnv2") @RequestParam(required = false) String cvn2) throws Exception {
        userBankService.doBindCard9pai(getLoginUser().getId(), phone, cardNo, cardType, expireDate, cvn2);
        return ok("绑卡成功");
    }

    /**
     * 解绑银行卡
     *
     * @param bankId 银行卡ID
     */
    @ApiOperation(value = "解绑银行卡", notes = "解绑银行卡")
    @ResponseBody
    @RequestMapping(value = "/unBindCard", method = RequestMethod.POST)
    public ResultEntity unBindCard(@ApiParam("银行卡ID") @RequestParam Integer bankId) throws Exception {
        userBankService.doUnBindCard(getLoginUser().getId(), bankId);
        return ok("解绑成功");
    }


    /**
     * 快捷支付发送验证码
     *
     * @param contractId 绑卡协议号
     * @param amount     充值金额
     */
    @ApiOperation(value = "快捷支付发送验证码", notes = "快捷支付发送验证码")
    @ResponseBody
    @RequestMapping(value = "/quickPayInit", method = RequestMethod.POST)
    public ResultEntity bindCard(HttpServletRequest request,
                                 @ApiParam("绑卡协议号") @RequestParam String contractId,
                                 @ApiParam("充值金额") @RequestParam BigDecimal amount) throws Exception {
        String notifyUrl = redisService.get(ConfigNID.WEB_SERVER_URL)+"api/notify/ninepai/doNotify";
        // 组装快捷支付参数
        Map<String, String> paramsMap = RpmUtils.buildQuickPayParams(getLoginUser().getId(), OrderNoUtils.getNPRechargeOrderNo(),
                contractId, amount, new Date(), getRequestIp(request), notifyUrl);
        // 发送短信验证码
        RpmUtils.doQuickPayInit(paramsMap);

        // 缓存充值信息
        redisService.put(CacheID.NINE_PAI_RECHARGE + getLoginUser().getId(), paramsMap);

        return ok("发送短信成功");
    }


    /**
     * 快捷支付提交
     *
     * @param checkCode 验证码
     */
    @ApiOperation(value = "快捷支付提交", notes = "快捷支付提交")
    @ResponseBody
    @RequestMapping(value = "/quickPayCommit", method = RequestMethod.POST)
    public ResultEntity bindCard(@ApiParam("验证码") @RequestParam String checkCode) throws Exception {
        rechargeService.doRechargeByNinePai(getLoginUser().getId(), checkCode);
        return ok("支付提交成功");
    }
}
